/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-03-14
 * Time: 22:35
 */

class ListNode {
    int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
 }

public class test2 {
    public ListNode addInList (ListNode head1, ListNode head2) {
        // write code here
        ListNode ret=new ListNode(0);
        int sum=0;
        int count=0;
        while (head1!= null || head2!= null) {
            if(head1!= null){
                sum+=head1.val;
                head1=head1.next;
            }
            if(head2!= null){
                sum+=head2.val;
                head2=head2.next;
            }
            if(sum/10!=0) {
                count++;
                sum = sum % 10;
            }
            ret.val=sum;
            ret.next=new ListNode(0);
            ret=ret.next;
            if(count==0){
                sum=0;
            }else{
                sum=count;
                count=0;
            }
        }
        if(sum!=0){
            ret.val=sum;
            ret.next=null;
        }
        //遍历反向输出
        ListNode temp=ret;
        while (temp!=null){
            System.out.print(temp.val);
            temp=temp.next;
        }
        return ret;
    }
}
